from django.db import models

# Create your models here.

class User(models.Model):
    ON=1
    OFF=2
    STATUS_CHOICES={
        (ON,'正常'),
        (OFF,'锁定')
    }
    class Meta:
        verdose_name_plural='用户列表'
    user_name=models.CharField(max_length=30,unique=True,verbose_name='用户名')
    pwd=models.CharField(max_length=200,verbose_name='密码')
    status=models.IntegerField(verbose_name='状态',choices=STATUS_CHOICES)
    phone=models.CharField(max_length=30,unique=True,verbose_name='手机号')

class Address(models.Model):
    user=models.ForeignKey('User',on_delete=models.CASCADE)
    user_name=models.CharField(max_length=20)
    phone=models.CharField(max_length=30,unique=True)
    address=models.CharField(max_length=200,null=False)
    is_default=models.IntegerField()

class Goods(models.Model):
    K='克'
    J='斤'
    G='个'
    UNIT_CHOICES={
        (K,'克'),
        (J,'斤'),
        (G,'个')
    }
    class Meta:
        verdose_name_plural = '商品表'
    good_name=models.CharField(max_length=30)
    imr_url=models.ImageField(upload_to='img')
    price=models.IntegerField()
    intro=models.TextField()
    type=models.ManyToManyField('Type')
    unit=models.CharField(max_length=30,choices=UNIT_CHOICES,default='克')

class Type(models.Model):
    class Meta:
        verdose_name_plural = '商品分类'
    type_name=models.CharField(max_length=30,unique=True)

    def __str__(self):
        return self.type_name


class Cart(models.Model):
    num=models.IntegerField()
    user=models.ForeignKey('User',on_delete=models.CASCADE)
    goods=models.ManyToManyField(Goods)

class Order(models.Model):
    order_no=models.CharField(max_length=30,unique=True)
    status=models.IntegerField()
    time=models.DateTimeField(auto_now_add=True)
    uesr_name=models.CharField(max_length=20)
    phone=models.CharField(max_length=30)
    address=models.CharField(max_length=200)
    user=models.ForeignKey('User',on_delete=models.CASCADE)
    total_price=models.IntegerField()
    pay_staus=models.IntegerField()

class OrderDetial(models.Model):
    good_name=models.CharField(max_length=30)
    price=models.IntegerField()
    num=models.IntegerField()
    img_url=models.CharField(max_length=100)
    total_price=models.IntegerField()
    order=models.ForeignKey('Order',on_delete=models.CASCADE)